package com.example.swagger2.controller;

import com.example.swagger2.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Api(value = "user", description = "用户管理", produces = MediaType.APPLICATION_JSON_VALUE)
@Controller
@RequestMapping("/user")
public class UserController
{

   //单个参数接受
   @ApiOperation(value = "获得用户列表", notes = "列表信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
   @ResponseBody
   @RequestMapping(value = "list")
   public User list(
         @ApiParam(value = "名字", required = true) @RequestParam String name, 
         @ApiParam(value = "密码", required = true) @RequestParam String password,
         @ApiParam(value = "token", required = true) @RequestParam String token )
   {
      User user = new User();
      user.setName( name );
      user.setPassword( password );
      user.setToken( token );
      return user;
   }

   //json形式接收
   @ApiOperation(value = "update用户", notes = "获取商品信息(用于数据同步)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
   @ResponseBody
   @RequestMapping(value = "update")
   public User update( @RequestBody User user )
   {
      User user2 = new User();
      user2 = user;
      return user2;
   }

   //对象形式接收 --有bug
   @ApiOperation(value = "添加用户", notes = "获取商品信息(用于数据同步)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
   @ResponseBody
   @RequestMapping(value = "add")
   public User add(@Validated @ModelAttribute User user )
   {
      User user2 = new User();
      user2 = user;
      return user2;
   }
   
   //其它标签
   @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息" , httpMethod = "POST" , produces = MediaType.APPLICATION_JSON_VALUE)
   @ApiImplicitParam(name = "name", value = "用户名字", required = true, dataType = "String")
/*   @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
      @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
})*/
   @ResponseBody
   @RequestMapping(value="/{name}")
   public User getUser(@PathVariable String name) {
      User user = new User();
      user.setName( name );
       return user;
   }
}